package com.online.teaching.module.common.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.online.teaching.module.common.blog.domain.dto.BlogAddDTO;
import com.online.teaching.module.common.blog.domain.dto.BlogUpdateDTO;
import com.online.teaching.module.common.blog.domain.dto.CommentAddDTO;
import com.online.teaching.module.common.blog.domain.vo.BlogGetDetailVO;
import com.online.teaching.module.common.blog.domain.vo.BlogGetVO;
import com.online.teaching.module.common.blog.domain.vo.CommonGetVO;
import com.online.teaching.module.common.blog.service.CommonBlogService;
import com.online.teaching.utils.Result;
import com.online.teaching.utils.StatusCode;
import com.online.teaching.utils.TokenDecode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName : CommonBlogController  //类名
 * @Description :   博客得通用模块//描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:20  //时间
 */

/**
 * 未测试
 */
@Api(tags ="老师以及学生博客模块")
@RestController
public class CommonBlogController {

    @Autowired
    private CommonBlogService commonBlogService;

    @Autowired
    private TokenDecode tokenDecode;


    @ApiOperation(value = "查询博客话题列表", notes = "查询博客话题列表")
    @GetMapping("common/blog/getBlogList")
    public Result<PageInfo<BlogGetVO>> getBlogList(@RequestParam("page") Integer page, @RequestParam("size")Integer size,@ApiParam("授课id")@RequestParam("teachesId")Integer teachesId)
    {
        PageInfo<BlogGetVO> blogList = commonBlogService.getBlogList(page, size, teachesId);
        return new Result<>(true, StatusCode.OK,"查询成功",blogList);
    }

    @ApiOperation(value = "发布新话题", notes = "发布新话题")
    @PostMapping("common/blog/addBlog")
    public Result addBlog(@RequestBody @Valid BlogAddDTO blogAddDTO)
    {
        int type=JSONObject.toJSONString(tokenDecode.getUserInfo().get("authorities")).charAt(2)-'0';
        Integer id=Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = commonBlogService.addBlog(blogAddDTO, type,id);
        return i==1?new Result(true, StatusCode.OK,"发布博客成功"):new Result(true,StatusCode.ERROR,"发布博客失败");
    }

    @ApiOperation(value = "获取该话题下的评论,点击查看博客详情时候调用", notes = "获取该话题下的评论，点击查看博客详情时调用")
    @GetMapping("common/blog/getBlogCommentList")
    public Result<List<CommonGetVO>> getBlogCommentList(@ApiParam("博客id")@RequestParam Integer id)
    {
        return new Result(true,StatusCode.OK,"查询成功",commonBlogService.getBlogCommentList(id));
    }

    @ApiOperation(value = "评论博客或者回复评论，如果commentId为空则为评论博客", notes = "评论博客或者回复评论，如果commentId为空则为评论博客")
    @PostMapping("common/blog/addBlogComment")
    public Result addBlogComment(@Valid @RequestBody CommentAddDTO commentAddDTO)
    {
        int type=JSONObject.toJSONString(tokenDecode.getUserInfo().get("authorities")).charAt(2)-'0';
        Integer id=Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = commonBlogService.addBlogComment(commentAddDTO, type, id);
        return i==1?new Result(true, StatusCode.OK,"发布评论成功"):new Result(true,StatusCode.ERROR,"发布评论失败");
    }


    @ApiOperation(value = "点击删除其发布的一条博客", notes = "点击删除其发布的一条博客")
    @GetMapping("common/blog/deleteBlog")
    public Result deleteBlog(@ApiParam("博客id")@RequestParam Integer id)
    {
        Integer userId=Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = commonBlogService.deleteBlog(userId, id);
        return i==1?new Result(true, StatusCode.OK,"删除博客成功"):new Result(true,StatusCode.ERROR,"删除博客失败");

    }

    @ApiOperation(value = "点击删除其发布的一条评论", notes = "点击删除其发布的一条评论")
    @GetMapping("common/blog/deleteBlogComment")
    public Result deleteBlogComment(@ApiParam("评论id")@RequestParam Integer id)
    {
        Integer userId=Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = commonBlogService.deleteBlogComment(userId, id);
        return i==1?new Result(true, StatusCode.OK,"删除评论成功"):new Result(true,StatusCode.ERROR,"删除评论失败");
    }

}
